class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isandroid: !0,
            cid: 1,
            cpid:0,
            sizehtml: [],
            sizeid: '',
            cphnum: 1,
            cate:'FW',
            amount: 835,
            page: 1,
            price: 825,
            list: [
                //0女 1男
             {
                name:'抹茶',
                age:20,
                gender:0,
                kg:46,
                cm:160,
                km:5.5,
                level:1,
                src:'./src/girl1.jpg',
                favor:25,
             },
             {
                name:'素素',
                age:21,
                 gender:0,
                kg:48,
                cm:159,
                km:2.5,
                level:2,
                src:'./src/girl2.jpg',
                favor:25,
             },
             {
                name:'欢欢',
                age:24,
                 gender:0,
                kg:51,
                cm:165,
                km:4.5,
                level:3,
                src:'./src/girl3.jpg',
                favor:25,
             },
             {
                name:'桃子',
                age:19,
                 gender:1,
                kg:48,
                cm:164,
                km:5.0,
                level:2,
                src:'./src/girl4.jpg',
                favor:25,
             },
            ],
            rid: 0,
            cpid:1,
            keyword: '',
            JX: '安卓',
            serverinput: '',
            
          
           
            isall: !1
        };
    }









    componentDidMount() {
        if(Constant.SWITCH_VCONSOLE){
            new VConsole();
        }

        let openid = localStorage.getItem('openid')
        // getuser(openid, res => {
        //     if (res.issuccess == 1) {
        //         let user = res.user
        //         this.setState({ user, allprice:835, amount:  835 })
        //     }
        // })

        // let sizehtml = [],cphtml=[], { sizelist } = this.state;

        // sizehtml.push(<option value={-1}>请选择</option>)
        // sizelist.forEach((x, i) => {
        //     sizehtml.push(
        //         <option key={i} value={x}>{x} </option>
        //     )
        // })
        // cphtml.push(<option value={1}>大包(￥535元)</option>)
        // cphtml.push(<option value={0}>小包(￥310元)</option>)
        // cphtml.push(<option value={-1}>不购买</option>)

        // this.setState({ sizehtml,openid,cphtml })

        //  console.log(liststr?'1':'2','liststr');
        // let list = JSON.parse(localStorage.getItem('orderlist')) || []
        // console.log(list, 'list');

        // this.setState({ list }, () => {
        //     this.getamount()
        // })
        // this.getdata()

    }

    pay(size){
        let{openid,amount} = this.state;
        praypay(openid,Number(amount)*100,size,res=>{
            console.log(res,'resres');
            let data = JSON.parse(res.ret)
            this.requestpay(data)
        })
        
    }

    requestpay(data){
        function onBridgeReady(){
 
         let sign = md5(`appId=wxa29d77bb2e0c40bb&nonceStr=${data.nonce_str}&package=${data.package}&signType=MD5&timeStamp=${data.timeStamp}&key=Y3A7lWq2Xsd7ScCTSp8QUqLiWELxHCl7`).toUpperCase()
             WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                   "appId":"wxa29d77bb2e0c40bb",     //公众号ID，由商户传入     
                   "timeStamp":data.timeStamp,         //时间戳，自1970年以来的秒数     
                   "nonceStr":data.nonce_str, //随机串     
                   "package":data.package,     
                   "signType":"MD5",         //微信签名方式：     
                   "paySign":sign //微信签名 
                },
 
                function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    
                // 使用以上方式判断前端返回,微信团队郑重提示：
                      //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                } 
             }); 
          }
          if (typeof WeixinJSBridge == "undefined"){
             if( document.addEventListener ){
                 document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
             }else if (document.attachEvent){
                 document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                 document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
             }
          }else{
             onBridgeReady();
          }
     }



    inputbz(e) {
        this.setState({ desc: e.target.value })
    }

    clear() {
        var b = confirm('确定要清空吗')
        if (b) {
            localStorage.removeItem('orderlist')
            this.setState({ list: [], desc: '', amount: 0 })
        }
    }

    gopay() {
        let { sizeid,openid } = this.state;
        if (sizeid == '' || sizeid == '请选择') {
            alert('请先选择尺寸')
        } else {

            canbuy(openid,res=>{
                if(res.data==true){
                    this.pay(sizeid)
                }else{
                    alert('请勿重复购买')
                }

            })
          // 
        }

    }

    selectchange(e) {
        let { value } = e.target
        this.setState({ sizeid: value })
    }

selectchange1(e) {
        let { value } = e.target,{amount,allprice} = this.state;
        if(value==0){
            amount=610
        }if(value==-1){
            amount=300
        }if(value==1){
            amount=835
        }
        this.setState({ cpid: value,amount })
    }

    clickcate(name){
        let {cate} = this.state
        if(cate!=name){
            this.setState({cate:name})
        }
    }
    toinfo(id){
        window.location.href='./cateinfo.html'
    }
    


    render() {
        let { list,cate } = this.state;
        console.log(list,'list');
        
        return (
            <div id="app">
             
                <div className="maincontent">
             
                <div className="list">
                    {
                        list.map((x,i)=>{
                            return(
                            <div onClick={this.toinfo.bind(this,x.id)} className="item">
                                <div className="left">
                                <img src={x.src} alt="" />
                                <div className="middle">
                                    <div className="topp">
                                    <p className="p1">{x.name}</p>
                                    <p className="p2">
                                        <img src={`./src/gender_${x.gender==0?'wo':''}man.png`} alt="" />
                                        <p>{x.kg}kg {x.cm}cm</p>
                                    </p>
                                    </div>
                                    <div className="favor">获得{x.favor}个好评</div>
                                </div>
                                </div>
                                <div className="last">
                                    <div className="xuanze">
                                        <img src="./src/location.png" alt="" />
                                        <p>距离{x.km}km</p>
                                    </div>
                                    <div className="xiadan">选择</div>
                                </div>
                            </div>
                            )
                        })
                    }
               

                </div>

                </div>
                  
               
            </div>
        )
    }
}

ReactDOM.render(
    <div>
        {/* <TopLine /> */}
        <div id='body'>
            <Tabbar index='2' />
            <Main />
        </div>
    </div>
    // <Barrage />
    , $('.container')[0]
)